/*
 * BibliotecaManagementFrame.java
 *
 * Created on 1 ottobre 2008, 17.09
 */
package it.biblioteca;

import it.biblioteca.util.Book;
import it.biblioteca.util.BookList;
import it.biblioteca.util.ComboFields;
import it.biblioteca.util.UtilFileOperation;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;

/**
 *
 * @author  giovine
 */
public class BibliotecaManagementFrame extends javax.swing.JFrame {

    BookList my_list;
    ComboFields my_fields;
    ArrayList <Book> found_books;

    /** Creates new form BibliotecaManagementFrame */
    public BibliotecaManagementFrame(BookList list, ComboFields fields) {
        this.my_list = list;
        this.my_fields = fields;
        initComponents();
        
        this.show_bt.setEnabled(false);
    }

    private void searchForTitle() {
        found_books = new ArrayList();
        
        ArrayList <Book> books = this.my_list.getEntireList();
        DefaultListModel list_model = new DefaultListModel();
        list_model.removeAllElements();
        String search_str = search_tf.getText();
        for(Book b : books){
            
            if( b.getTitle().toLowerCase().contains(search_str.toLowerCase())){
                System.out.println("Libro trovato: "+b.getTitle());
                found_books.add(b);
            }
        }
        
        System.out.println("Dimensione prima del for: "+found_books.size());
        for(Book b : found_books){
            list_model.addElement(b.getTitle());
        }
        this.found_list.setModel(list_model);
        
        this.result_label.setText("Risultati trovati: "+found_books.size());
        if(found_books.size() > 0){
            this.show_bt.setEnabled(true);
        }
    }

    private void searchForAuthor() {
        found_books = new ArrayList();
        
        ArrayList <Book> books = this.my_list.getEntireList();
        DefaultListModel list_model = new DefaultListModel();
        list_model.removeAllElements();
        
        for(Book b : books){
            if(b.getAuthor().toLowerCase().contains(this.search_tf.getText().toLowerCase())){
                found_books.add(b);
            }
        }
        
        for(Book b : found_books){
            list_model.addElement(b.getTitle());
        }
        this.found_list.setModel(list_model);
        
        this.result_label.setText("Risultati trovati: "+found_books.size());
        if(found_books.size() > 0){
            this.show_bt.setEnabled(true);
        }
    }
    
    private void searchForHouse(){
        found_books = new ArrayList();
        
        ArrayList <Book> books = this.my_list.getEntireList();
        DefaultListModel list_model = new DefaultListModel();
        list_model.removeAllElements();
        
        for(Book b : books){
            if(b.getHouse().toLowerCase().contains(this.search_tf.getText().toLowerCase())){
                found_books.add(b);
            }
        }
        
        for(Book b : found_books){
            list_model.addElement(b.getTitle());
        }
        this.found_list.setModel(list_model);
        
        this.result_label.setText("Risultati trovati: "+found_books.size());
        if(found_books.size() > 0){
            this.show_bt.setEnabled(true);
        }
    }
    
    private void searchForTranslater(){
        found_books = new ArrayList();
        
        ArrayList <Book> books = this.my_list.getEntireList();
        DefaultListModel list_model = new DefaultListModel();
        list_model.removeAllElements();
        
        for(Book b : books){
            if(b.getTranslater().toLowerCase().contains(this.search_tf.getText().toLowerCase())){
                found_books.add(b);
            }
        }
        
        for(Book b : found_books){
            list_model.addElement(b.getTitle());
        }
        this.found_list.setModel(list_model);
        
        this.result_label.setText("Risultati trovati: "+found_books.size());
        if(found_books.size() > 0){
            this.show_bt.setEnabled(true);
        }
    }
    
    private void searchForLocation(){
        found_books = new ArrayList();
        
        ArrayList <Book> books = this.my_list.getEntireList();
        DefaultListModel list_model = new DefaultListModel();
        list_model.removeAllElements();
        
        for(Book b : books){
            if(b.getLocation().equalsIgnoreCase(this.search_tf.getText())){
                found_books.add(b);
            }
        }
        
        for(Book b : found_books){
            list_model.addElement(b.getTitle());
        }
        this.found_list.setModel(list_model);
        
        this.result_label.setText("Risultati trovati: "+found_books.size());
        if(found_books.size() > 0){
            this.show_bt.setEnabled(true);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        found_list = new javax.swing.JList();
        show_bt = new javax.swing.JButton();
        search_tf = new javax.swing.JTextField();
        search_bt = new javax.swing.JButton();
        close_bt = new javax.swing.JButton();
        kind_combo = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        result_label = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        file_menu = new javax.swing.JMenu();
        save_item = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        close_item = new javax.swing.JMenuItem();
        edit_menu = new javax.swing.JMenu();
        edit_item = new javax.swing.JMenuItem();
        help_menu = new javax.swing.JMenu();
        info_item = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("BiblioTeCa - Gestione libri");
        setIconImage(new ImageIcon("files/icon_desktop.png").getImage());
        setResizable(false);

        jScrollPane1.setViewportView(found_list);

        show_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_modify.png"))); // NOI18N
        show_bt.setText("Mostra Dettagli");
        show_bt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                show_btActionPerformed(evt);
            }
        });

        search_tf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                search_tfActionPerformed(evt);
            }
        });

        search_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_search.png"))); // NOI18N
        search_bt.setText("Cerca");
        search_bt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                search_btActionPerformed(evt);
            }
        });

        close_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_close.png"))); // NOI18N
        close_bt.setText("Chiudi");
        close_bt.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                close_btActionPerformed(evt);
            }
        });

        kind_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Titolo del libro", "Autore del libro", "Casa editrice", "Traduttore", "Locazione" }));

        jLabel1.setText("Ricerca per:");

        result_label.setText("Risultati trovati:");

        file_menu.setText("File");

        save_item.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        save_item.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_save.png"))); // NOI18N
        save_item.setText("Salva Database");
        save_item.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                save_itemActionPerformed(evt);
            }
        });
        file_menu.add(save_item);
        file_menu.add(jSeparator1);

        close_item.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        close_item.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_close.png"))); // NOI18N
        close_item.setText("Chiudi");
        close_item.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                close_itemActionPerformed(evt);
            }
        });
        file_menu.add(close_item);

        jMenuBar1.add(file_menu);

        edit_menu.setText("Modifica");

        edit_item.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_E, java.awt.event.InputEvent.CTRL_MASK));
        edit_item.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_edit.png"))); // NOI18N
        edit_item.setText("Modifica locazioni");
        edit_item.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                edit_itemActionPerformed(evt);
            }
        });
        edit_menu.add(edit_item);

        jMenuBar1.add(edit_menu);

        help_menu.setText("Aiuto");

        info_item.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        info_item.setIcon(new javax.swing.ImageIcon(getClass().getResource("/it/biblioteca/icons/icon_info.png"))); // NOI18N
        info_item.setText("Informazioni");
        info_item.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                info_itemActionPerformed(evt);
            }
        });
        help_menu.add(info_item);

        jMenuBar1.add(help_menu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 287, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 72, Short.MAX_VALUE)
                                .addComponent(close_bt)
                                .addGap(28, 28, 28))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(6, 6, 6)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(result_label)
                                    .addComponent(show_bt)))))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(kind_combo, javax.swing.GroupLayout.PREFERRED_SIZE, 230, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(search_tf, javax.swing.GroupLayout.PREFERRED_SIZE, 377, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(search_bt)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(search_tf, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(search_bt))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(kind_combo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(show_bt)
                        .addGap(27, 27, 27)
                        .addComponent(result_label))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 262, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(close_bt)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void close_btActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_close_btActionPerformed
    UtilFileOperation.saveDataBase(my_list);
    dispose();
    BibliotecaInitialChoice init = new BibliotecaInitialChoice(this.my_list, this.my_fields);
    init.setLocationRelativeTo(null);
    init.setVisible(true);
}//GEN-LAST:event_close_btActionPerformed

private void search_btActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_search_btActionPerformed
    int selection = this.kind_combo.getSelectedIndex();
    System.out.println("Hai scelto: " + selection);

    switch (selection) {
        case 0: //Search for title.
            searchForTitle();break;
        case 1: //Search for author
            searchForAuthor();break;
        case 2: //Search for house
            searchForHouse();break;
        case 3: //Search for translater
            searchForTranslater();break;
        case 4: //Search for location
            searchForLocation();break;
        default: //Do nothing
            break;
    }
}//GEN-LAST:event_search_btActionPerformed

private void show_btActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_show_btActionPerformed
    
    BibliotecaShowDetails details = new BibliotecaShowDetails(this.my_list, this.found_list.getSelectedIndex(),this.found_books, this.my_fields);
    details.setLocationRelativeTo(null);
    details.setVisible(true);
    
}//GEN-LAST:event_show_btActionPerformed

private void save_itemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_save_itemActionPerformed
    UtilFileOperation.saveDataBase(my_list);
        try {
            this.my_list.setBookList(UtilFileOperation.getBookList());//GEN-LAST:event_save_itemActionPerformed
        } catch (FileNotFoundException ex) {
            Logger.getLogger(BibliotecaManagementFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
}                                         

private void close_itemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_close_itemActionPerformed
    UtilFileOperation.saveDataBase(my_list);
    dispose();
    BibliotecaInitialChoice init = new BibliotecaInitialChoice(this.my_list, this.my_fields);
    init.setLocationRelativeTo(null);
    init.setVisible(true);
}//GEN-LAST:event_close_itemActionPerformed

private void info_itemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_info_itemActionPerformed
    BibliotecaInfoFrame info = new BibliotecaInfoFrame();
    info.setLocationRelativeTo(null);
    info.setVisible(true);
}//GEN-LAST:event_info_itemActionPerformed

private void edit_itemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_edit_itemActionPerformed
    BibliotecaEditLocationFrame edit_location = new BibliotecaEditLocationFrame(this.my_fields,this,false);
    edit_location.setLocationRelativeTo(null);
    edit_location.setVisible(true);
}//GEN-LAST:event_edit_itemActionPerformed

private void search_tfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_search_tfActionPerformed
    
    int selection = this.kind_combo.getSelectedIndex();
    

    switch (selection) {
        case 0: //Search for title.
            searchForTitle();break;
        case 1: //Search for author
            searchForAuthor();break;
        case 2: //Search for house
            searchForHouse();break;
        case 3: //Search for translater
            searchForTranslater();break;
        case 4: //Search for location
            searchForLocation();break;
        default: //Do nothing
            break;
    }
}//GEN-LAST:event_search_tfActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton close_bt;
    private javax.swing.JMenuItem close_item;
    private javax.swing.JMenuItem edit_item;
    private javax.swing.JMenu edit_menu;
    private javax.swing.JMenu file_menu;
    private javax.swing.JList found_list;
    private javax.swing.JMenu help_menu;
    private javax.swing.JMenuItem info_item;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JComboBox kind_combo;
    private javax.swing.JLabel result_label;
    private javax.swing.JMenuItem save_item;
    private javax.swing.JButton search_bt;
    private javax.swing.JTextField search_tf;
    private javax.swing.JButton show_bt;
    // End of variables declaration//GEN-END:variables
}
